#include <bits/stdc++.h>
// 2024/12/14
// tag: 
using namespace std;

const int N = 2505;
int n, t[N], T[N], ans, u, v;
vector<int> e[N];

void dfs(int a, int b){
    for (auto x: e[a]){
        if (x == b)
            continue;
        dfs(x, a);
        t[a] = (t[a] - t[x] + 12) % 12;
    }
}

signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> T[i], T[i] %= 12;
    for (int i = 2; i <= n; i++){
        cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    for (int i = 1; i <= n; i++){
        memcpy(t, T, sizeof(t));
        dfs(i, 0);
        if (t[i] == 0 || t[i] == 1)
            ans++;
    }
    cout << ans << endl;
    return 0;
}